<template>
    <div class="boxwrapper">
        <el-form :inline="true" size="small" :model="ruleForm" class="demo-form-inline">
            <el-form-item label="开发商：">
                <el-input v-model.trim="ruleForm.companyName" placeholder="请输入开发商"></el-input>
            </el-form-item>
            <el-form-item label="城市：">
                <el-select v-model="ruleForm.cityId" filterable>
                    <el-option v-for="item in citys"
                               :key="item.cityId"
                               :label="item.city"
                               :value="item.cityId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="楼盘名称：">
                <el-select v-model="ruleForm.devName" filterable>
                    <el-option v-for="item in mappedDevList"
                               :key="item.devId"
                               :label="item.devName"
                               :value="item.devName">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="佣金支付方式：">
                <el-select v-model="ruleForm.commissionPayWay" filterable>
                    <el-option v-for="item in commissionPayWaysList"
                               :key="item.code"
                               :label="item.name"
                               :value="item.code">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="外部地区名称：">
                <el-select v-model="ruleForm.outsideRegionId" filterable>
                    <el-option v-for="item in externalRegionList"
                               :key="item.id"
                               :label="item.regionName"
                               :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchList" id="monitorEnter">查询</el-button>
                <el-button @click="resetfrom">重置</el-button>
            </el-form-item>
        </el-form>
        <div class="mb20">
            <el-button size="small" type="primary" @click="newCreate">新建</el-button>
        </div>

        <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
            <el-table-column prop="companyName" label="开发商" align="center"></el-table-column>
            <el-table-column prop="city" label="城市" align="center"></el-table-column>
            <el-table-column label="运营后台" align="center">
                <el-table-column label="项目" prop="projectName" align="center">
                </el-table-column>
                <el-table-column label="楼盘" prop="devName" align="center">
                </el-table-column>
                <el-table-column label="楼盘id" prop="devId" align="center">
                </el-table-column>
            </el-table-column>
            <el-table-column label="明源CRM" align="center">
                <el-table-column label="项目" prop="projName" align="center">
                </el-table-column>
                <el-table-column label="项目id" prop="projCode" align="center">
                </el-table-column>
            </el-table-column>
            <el-table-column prop="outsideRegionName" label="外部地区名称" align="center"></el-table-column>
            <el-table-column prop="commissionPayWayName" label="佣金支付方式" align="center"></el-table-column>
            <el-table-column prop="mappingHouseNum" label="已映射房源" align="center">
                <template slot-scope="scope">
                    <el-button type="text" @click="goDetail(scope.row)">{{scope.row.mappingHouseNum}}</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="city" label="操作" align="center" fixed="right">
                <template slot-scope="scope">
                    <el-button type="text" @click="edit(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagebar">
            <el-pagination
                    background
                    layout="total, sizes, prev, pager, next"
                    :current-page.sync="ruleForm.pageNum"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="ruleForm.pageSize"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :total="totalSize">
            </el-pagination>
        </div>
        <el-dialog
                title="映射楼盘信息"
                :visible.sync="dialogVisible"
                center
                width="600px">
            <table class="dialog-table" border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <th>映射信息</th>
                    <th>好生活楼盘信息</th>
                    <th>明源CRM</th>
                </tr>
                <tr>
                    <td>地区</td>
                    <td>
                        <el-select size="small" :disabled="isEditDialog" v-model="dialogForm.cityId" @change="getProjectList" filterable>
                            <el-option v-for="item in cityList"
                                       :key="item.cityId"
                                       :label="item.city"
                                       :value="item.cityId">
                            </el-option>
                        </el-select>
                    </td>
                    <td>
                        <!-- 明源 -->
                        <el-select size="small" :disabled="isEditDialog" v-model="dialogForm.groupCode" @change="getCrmProjectList" filterable>
                            <el-option v-for="item in crmGroupList"
                                       :key="item.Code"
                                       :label="item.Group_Name"
                                       :value="item.Code">
                            </el-option>
                        </el-select>
                    </td>
                </tr>
                <tr>
                    <td>项目</td>
                    <td>
                        <el-select size="small" :disabled="!dialogForm.cityId || isEditDialog" v-model="dialogForm.projectId" @change="getDevList" filterable>
                            <el-option v-for="item in projectList"
                                       :key="item.estateProjectId"
                                       :label="item.projectName"
                                       :value="item.estateProjectId">
                            </el-option>
                        </el-select>
                    </td>
                    <td>
                        <!-- 明源 -->
                        <el-select size="small" :disabled="!dialogForm.groupCode || isEditDialog" v-model="dialogForm.projCode" filterable>
                            <el-option v-for="item in crmProjectList"
                                       :key="item.Proj_Code"
                                       :label="item.Proj_Name"
                                       :value="item.Proj_Code">
                            </el-option>
                        </el-select>
                    </td>
                </tr>
                <tr>
                    <td>楼盘</td>
                    <td>
                        <el-select size="small" :disabled="!dialogForm.projectId || isEditDialog" v-model="dialogForm.devId" filterable>
                            <el-option v-for="item in devList"
                                       :key="item.devId"
                                       :label="item.devName"
                                       :value="item.devId">
                            </el-option>
                        </el-select>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td>ID</td>
                    <td>{{dialogForm.devId}}</td>
                    <td>{{dialogForm.projCode}}</td>
                </tr>
            </table>
            <el-form :model="dialogForm" size="small" :rules="dialogRules" ref="dialogForm" label-width="135px" class="mt20 demo-ruleForm">
                <el-form-item label="关联外部地区名称" prop="regionId">
                    <el-select v-model="dialogForm.regionId" placeholder="请选择外部地区">
                        <el-option v-for="item in externalRegionList"
                                   :key="item.id"
                                   :label="item.regionName"
                                   :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="佣金支付方式" prop="commissionPayWay">
                    <el-radio v-model="dialogForm.commissionPayWay" v-for="item in commissionPayWaysList" :key="item.code" :label="item.code">
                        {{item.name}}
                    </el-radio>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveMappingInfo">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    import api from '@/api/apiPath'

    export default {
        data() {
            return {
                ruleForm: {
                    commissionPayWay: '', // 佣金支付方式
                    devName: "",//楼盘名称
                    companyName: "",//开发商
                    cityId: '',//城市id
                    showProject: 5,//珠江楼盘
                    projName: "",
                    outsideRegionId: '',
                    pageNum: 1,
                    pageSize: 10
                },
                citys: [],
                totalSize: 0,
                tableData: [],
                dialogVisible: false,
                dialogForm: {
                    cityId: '',
                    projectId: '',
                    devId: '',
                    groupCode: '',
                    projCode: '',
                    regionId: '',
                    commissionPayWay: 1
                },
                dialogRules: {
                    regionId: [
                        {required: true, message: '请选择外部地区名称', trigger: 'blur'},
                    ],
                    commissionPayWay: [
                        {required: true, message: '请选择佣金支付方式', trigger: 'blur'},
                    ],
                },

                cityList: [],
                projectList: [],
                devList: [],
                crmGroupList: [],
                crmProjectList: [],
                externalRegionList: [],
                commissionPayWaysList: [],
                mappedDevList: [],
                isEditDialog: false
            }
        },
        methods: {
            goDetail(row) {
                if(row.mappingHouseNum <= 0){
                    this.$message('当前楼盘没有映射房源')
                    return false
                }
                this.$router.push({path: '/mappedHouseList', query: {code: row.projCode}})
            },
            loadCity() {
                this.axios.post(api.CITYLIST).then(res => {
                    if (res.data.code !== 200) {
                        this.$message(res.data.msg)
                    } else {
                        this.citys = res.data.data.cityList
                    }
                })
            },
            newCreate() {
                this.dialogForm = {
                    cityId: '',
                    projectId: '',
                    devId: '',
                    groupCode: '',
                    projCode: '',
                    regionId: '',
                    commissionPayWay: 1
                }
                this.dialogVisible = true;
                this.isEditDialog = false;
            },
            edit(row) {
                console.log(row);
                this.isEditDialog = true;
                // this.externalRegionList = [{
                //     regionName: row.outsideRegionName,
                //     id: row.outsideRegionId
                // }]
                this.devList = [{
                    devId: row.devId,
                    devName: row.devName
                }]
                this.crmProjectList = [{
                    Proj_Name: row.projName,
                    Proj_Code: row.projCode
                }]
                this.projectList = [{
                    estateProjectId: row.projectId,
                    projectName: row.projectName
                }]

                this.dialogForm = {
                    cityId: row.cityId,
                    projectId: row.projectId,
                    devId: row.devId,
                    groupCode: row.groupCode,
                    projCode: row.projCode,
                    regionId: row.outsideRegionId,
                    commissionPayWay: row.commissionPayWay
                };
                this.dialogVisible = true;
            },

            resetfrom() {
                this.ruleForm = {
                    commissionPayWay: '', // 佣金支付方式
                    devName: "",//楼盘名称
                    companyName: "",//开发商
                    cityId: '',//城市id
                    showProject: 5,//珠江楼盘
                    projName: "",
                    outsideRegionId: '',
                    pageNum: 1,
                    pageSize: 10
                }
            },
            searchList() {
                this.ruleForm.pageNum = 1;
                this.loadData();
            },
            handleSizeChange(val) {
                this.ruleForm.pageSize = val;
                this.loadData();
            },
            handleCurrentChange(val) {
                this.ruleForm.pageNum = val;
                this.loadData();
            },
            loadData() {
                this.$axios.post("/forcrm/projectMapping/list", this.ruleForm).then(res => {
                    this.tableData = res.rows;
                    this.totalSize = res.total;
                })
            },
            getCityList() {
                this.$axios.post('/forcrm/projectMapping/getCityList').then(res => {
                    this.cityList = res
                })
            },
            getProjectList() {
                this.dialogForm.projectId = '';
                this.dialogForm.devId = '';
                this.$axios.post('/forcrm/projectMapping/getProjectList', {
                    cityId: this.dialogForm.cityId
                }).then(res => {
                    this.projectList = res
                })
            },
            getDevList() {
                this.dialogForm.devId = '';
                this.$axios.post('/forcrm/projectMapping/getDevList', {
                    projectId: this.dialogForm.projectId
                }).then(res => {
                    this.devList = res
                })
            },

            getCrmGroupList() {
                this.$axios.post('/forcrm/projectMapping/getCrmGroupList', {}).then(res => {
                    this.crmGroupList = res
                })
            },
            getCrmProjectList() {
                this.dialogForm.projCode = '';
                this.$axios.post('/forcrm/projectMapping/getCrmProjectList', {
                    Code: this.dialogForm.groupCode
                }).then(res => {
                    this.crmProjectList = res
                })
            },
            getExternalRegionList() {
                this.$axios.post('/commission/externalRegion/list', {
                    pageNum: 1,
                    pageSize: 99999
                }).then(res => {
                    this.externalRegionList = res.list
                })
            },
            getCommissionPayWays() {
                this.$axios.post('/forcrm/projectMapping/getCommissionPayWays', {}).then(res => {
                    this.commissionPayWaysList = res
                })
            },
            saveMappingInfo() {
                if (!this.dialogForm.devId) {
                    this.$message.warning("请选择好生活楼盘信息")
                    return false
                }
                if (!this.dialogForm.groupCode) {
                    this.$message.warning("请选择明源CRM地区信息")
                    return false
                }
                if (!this.dialogForm.projCode) {
                    this.$message.warning("请选择明源CRM项目信息")
                    return false
                }
                this.$refs.dialogForm.validate((valid) => {
                    if (valid) {
                        let projName = this.crmProjectList.find((item) => {
                            return item.Proj_Code === this.dialogForm.projCode
                        }).Proj_Name
                        let groupName = this.crmGroupList.find((item) => {
                            return item.Code === this.dialogForm.groupCode
                        }).Group_Name

                        let outsideRegionName = this.externalRegionList.find((item) => {
                            return item.id === this.dialogForm.regionId
                        }).regionName
                        let params = {
                            projCode: this.dialogForm.projCode,
                            projName,
                            devId: this.dialogForm.devId,
                            groupCode: this.dialogForm.groupCode,
                            groupName,
                            commissionPayWay: this.dialogForm.commissionPayWay,
                            outsideRegionId: this.dialogForm.regionId,
                            outsideRegionName
                        }
                        this.$axios.post('/forcrm/projectMapping/saveMappingInfo', params).then(res => {
                            this.dialogVisible = false;
                            this.$message.success("保存成功")
                            this.loadData();
                        })
                    }
                });
            },
            getMappedDevList() {
                this.$axios.post('/forcrm/projectMapping/getMappedDevList', {}).then(res => {
                    this.mappedDevList = res
                })
            },

        },
        created() {
            this.ruleForm.outsideRegionId = +this.$route.query.outsideRegionId || '';
            this.loadCity();
            this.loadData();
            this.getMappedDevList()
            this.getCityList()
            this.getCrmGroupList()
            this.getExternalRegionList()
            this.getCommissionPayWays()
        }
    }
</script>
<style lang="stylus" scoped>
    .dialog-table {
        width: 100%;
        text-align: center;
        border-collapse: collapse;
        border: 1px solid #EBEEF5;
    }
</style>
